package dao.oc_news;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.lob.SerializableClob;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import tools.Pager;

import entitybean.oc_news.EntityBeanOc_News;
import entitybean.oc_study.EntityBeanOc_Study;


public class DaoImpl_Oc_News extends HibernateDaoSupport implements Dao_Oc_News{
	
	public void Delete_Oc_News(EntityBeanOc_News e)
	{
		this.getHibernateTemplate().delete(e);
	}
	
	public void Insert_Oc_News(final EntityBeanOc_News e0) {
		try {
			getHibernateTemplate().execute(new HibernateCallback() {
				@SuppressWarnings("deprecation")
				public Object doInHibernate(Session arg0)
						throws HibernateException, SQLException {
					// 1 Ҫ����һ��ֻ��" "��clob����
					EntityBeanOc_News e1 = e0;
					e1.setNews_clob(Hibernate.createClob(" "));
					arg0.save(e1);
					arg0.flush();
					// 2 ִ��select for update����,Ҳ���Ǽ��м�������
					arg0.refresh(e1, LockMode.UPGRADE);
					// 3 ģ���α�Ļ��ƣ�д��ʵ�ʵ�����
					SerializableClob sclob = (SerializableClob) e1.getNews_clob();
					java.sql.Clob jclob = sclob.getWrappedClob();
					oracle.sql.CLOB clob = (oracle.sql.CLOB) jclob;
					try {
						Writer out = clob.getCharacterOutputStream();
						out.write(e1.getNews_clob_string());
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					return null;
				}
			});
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	
	
	@SuppressWarnings("unchecked")
	public EntityBeanOc_News Find_Oc_News_By_Id(String id)
	{
		EntityBeanOc_News e=new EntityBeanOc_News();
		List<EntityBeanOc_News>list=this.getHibernateTemplate().find("from EntityBeanOc_News where del='OFF' and id="+id);
		if(list.size()>0)
		{
			e=list.get(0);
		}
		return e;
	}
	
	
	
	
	
	
	
	
	
	
	/*public void Insert_Oc_News(final  EntityBeanOc_News e)
	{
		try {
			getHibernateTemplate().execute(new HibernateCallback() {
				@SuppressWarnings("deprecation")
				public Object doInHibernate(Session arg0)
						throws HibernateException, SQLException {
					// 1 Ҫ����һ��ֻ��" "��clob����
					EntityBeanOc_News e1 = e;
					e.setNews_clob(Hibernate.createClob(" "));
					arg0.save(e1);
					arg0.flush();
					// 2 ִ��select for update����,Ҳ���Ǽ��м�������
					arg0.refresh(e, LockMode.UPGRADE);
					// 3 ģ���α�Ļ��ƣ�д��ʵ�ʵ�����
					SerializableClob sclob = (SerializableClob) e1.getNews_clob();
					java.sql.Clob jclob = sclob.getWrappedClob();
					oracle.sql.CLOB clob = (oracle.sql.CLOB) jclob;
					try {
						Writer out = clob.getCharacterOutputStream();
						out.write(e.getNews_clob_string());
						out.close();
					} catch (IOException e0) {
						e0.printStackTrace();
					}
					return null;
				}
			});
		} catch (RuntimeException re) {
			throw re;
		}
		
		
	}*/
	public void Update_Oc_News(EntityBeanOc_News e){this.getHibernateTemplate().update(e);}
	public void Delete_Oc_News(String id)
	{
		List<EntityBeanOc_News> list=this.getHibernateTemplate().find("from EntityBeanOc_News where del='OFF' and id in("+id+")");	
		for(int i=0;i<list.size();i++)
		{
			EntityBeanOc_News e=list.get(i);
			e.setDel("ON");
			this.getHibernateTemplate().update(e);
			
		}
	}
	@SuppressWarnings("unchecked")
	public List<EntityBeanOc_News> Find_Oc_News(){
		return this.getHibernateTemplate().find("from EntityBeanOc_News where del='OFF' ");
		}
	
	public List<EntityBeanOc_News> Find_Oc_News_Index(Integer i)
	{
		
		List<EntityBeanOc_News> list=new ArrayList<EntityBeanOc_News>();
		list=this.getHibernateTemplate().find("from EntityBeanOc_News where del='OFF' order by update_time desc");
		return list;
		
	}
	
	
	
	Pager pager; 
	public Pager getPager() { 
	return pager; 
	} 
	public void setPager(Pager page) { 
	this .pager = page; 
	} 
	public List getProductByPage() { 
	// TODO Auto-generated method stub 
	return getHibernateTemplate().executeFind( new HibernateCallback(){ 
	public Object doInHibernate(Session session) throws HibernateException, SQLException { 
	Query query=session.createQuery("from " +pager.getPageName()+ " where " +pager.getConString()); 
	query.setFirstResult(pager.getBeginIndex()); //hibernate��ҳ�ľ��� 
	query.setMaxResults(pager.getEveryPage()); 
	return query.list(); 
	} 
	}); 
	} 
	public List getProducts(){ 
	return getHibernateTemplate().find( " from " +pager.getPageName()+ " where " +pager.getConString()); 
	} 
	public int getProductCount() { 
	// TODO Auto-generated method stub
	List list=getHibernateTemplate().find(" from " +pager.getPageName()+ " where " +pager.getConString()); 
	return ((Integer)list.size()).intValue(); 
	}

	public List<EntityBeanOc_News> Find_Oc_News(final Integer top) {
		return getHibernateTemplate().executeFind( new HibernateCallback(){ 
			public Object doInHibernate(Session session) throws HibernateException, SQLException { 
			Query query=session.createQuery("from EntityBeanOc_News  where del='OFF' order by id desc"); 
			query.setFirstResult(0);
			query.setMaxResults(top); 
			return query.list(); 
			} 
	   });
	} 


}
